package com.knife.blog.logic.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.knife.blog.domain.BlogInfo;
import com.knife.blog.domain.Music;
import com.knife.blog.logic.BlogMusicService;
import com.knife.web.tools.DbPageList;
import com.knife.web.tools.IPageList;

public class BlogMusicServiceImpl extends DAOSupportService implements BlogMusicService {

    private static BlogMusicService musicService = new BlogMusicServiceImpl();

    public static BlogMusicService getInstance() {
        return musicService;
    }

    public List queryBlogMusic(BlogInfo blog) {
        List musicList = new ArrayList();
        Collection<Object> params = new ArrayList<Object>();
        params.add(blog.getUsername());
        musicList = dao.query(Music.class, "inputuser=?", params);
        return musicList;
    }

    public IPageList queryBlogMusic(String username, String key, int currentPage,
            int pageSize) {
        DbPageList pList;
        String scope = "inputuser=? order by inputtime desc";
        java.util.Collection<Object> paras = new java.util.ArrayList<Object>();
        paras.add(username);
        pList = new DbPageList(Music.class, scope, paras);
        pList.doList(currentPage, pageSize);
        return pList;
    }

    public Music getMusic(String cid) {
        return (Music) dao.get(Music.class, cid);
    }

    public boolean addMusic(Music music) {
        boolean ret;
        music.setInputtime(new java.util.Date());
        music.setState(new Integer(0));
        ret = this.dao.save(music);
        if (ret) {
            try {
                BlogInfo blog = (BlogInfo) BlogUserServiceImpl.getInstance().getUserBlog(music.getInputuser());
                blog.setUpdated(Boolean.TRUE);
                //HTMLGenerater.saveToHTML(music);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return ret;
    }

    public boolean delMusic(String cid) {
        boolean ret;
        ret = this.dao.del(getMusic(cid));
        return ret;
    }

    public boolean updateMusic(Music obj) {
        boolean ret;
        ret = this.dao.update(obj);
        return ret;
    }

    public int batchDelMusic(String[] cid) {
        int ret = 0;
        for (int i = 0; i < cid.length; i++) {
            if (this.dao.del(cid[i])) {
                ret++;
            }
        }
        return ret;
    }
}
